package gang.com.mobelhelper.ui.fragment;

import android.app.Application;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import gang.com.mobelhelper.AppApplication;
import gang.com.mobelhelper.R;
import gang.com.mobelhelper.bean.AppInfo;
import gang.com.mobelhelper.di.DaggerRecommendCompent;
import gang.com.mobelhelper.di.RemmendMoudle;
import gang.com.mobelhelper.di.component.AppCompent;
import gang.com.mobelhelper.presenter.RecommendPresenter;
import gang.com.mobelhelper.presenter.contract.RecommendContract;
import gang.com.mobelhelper.ui.activity.AppDetailActivity;
import gang.com.mobelhelper.ui.adapter.RecomendAppAdatper;

/**
 * 推荐
 * Created by xiaogangzai on 2017/7/10.
 */

public class RecommendFragment extends BaseFragment<RecommendPresenter> implements RecommendContract.View {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    private RecomendAppAdatper recomendAppAdatper;
    //注意在注入的时候不要定义为私有的，私有是不可以注入的,下面这句话的意思是告诉Dagger2我们需要这个对象
   /* @Inject
    RecommendContract.Presenter mPresenter;*/
    @Inject
    ProgressDialog mProgressDialog;

    /*@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recomend, container, false);
        unbinder = ButterKnife.bind(this, view);
        //mProgressDialog = new ProgressDialog(getActivity());
        //这里我们要new接口的实现类，没有使用Dagger2之前都是必须new出来的
        // mPresenter = new RecommendPresenter(this);
        //下面两句话意义相同
        //DaggerRecommendCompent.builder().remmendMoudle(new RemmendMoudle(this)).build().inject(this);
        DaggerRecommendCompent.builder().appCompent(((AppApplication) getActivity().getApplication()).getmAppComponent()).remmendMoudle(new RemmendMoudle(this)).build().inject(this);
        initData();
        return view;
    }*/
    @Override
    public int setLayoutId() {
        return R.layout.fragment_recomend;
    }

    @Override
    public void setupActivityComponent(AppCompent appCompent) {
        DaggerRecommendCompent.builder().appCompent(appCompent)
                .remmendMoudle(new RemmendMoudle(this))
                .build().inject(this);
    }

    @Override
    public void init() {
        //这里先权限判断
        mPresenter.request_pression();

    }

    /*
    使用MVP之后将其省略掉
    private void initData() {
        HttpManage httpmange = new HttpManage();
        ApiService apiService = httpmange.getRetrofit(httpmange.getOkHttpClient()).create(ApiService.class);
        apiService.getApps("{'page':0}").enqueue(new Callback<PageBean<AppInfo>>() {
            @Override
            public void onResponse(Call<PageBean<AppInfo>> call, Response<PageBean<AppInfo>> response) {
                PageBean<AppInfo> pagebean = response.body();
                List<AppInfo> datas = pagebean.getDatas();
                initRecyclerView(datas);
            }
            @Override
            public void onFailure(Call<PageBean<AppInfo>> call, Throwable t) {

            }
        });
    }*/

    private void initRecyclerView(List<AppInfo> datas) {
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        recomendAppAdatper = new RecomendAppAdatper(getActivity(), datas);
        recycleView.setAdapter(recomendAppAdatper);
        recomendAppAdatper.setOnItemClickListener(new RecomendAppAdatper.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                mApplication.setmView(view);
                startActivity(new Intent(getActivity(), AppDetailActivity.class));
            }
        });
    }


    @Override
    public void showLoading() {
        mProgressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showResult(List<AppInfo> datas) {
        initRecyclerView(datas);
    }

    @Override
    public void showNodata() {
        Toast.makeText(getActivity(), "暂时无数据，请吃完饭再来", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showErorrdata(String msg) {
        Toast.makeText(getActivity(), "服务器开小差了：" + msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void Permission_Sucess() {
        //权限成功。以后这里通知Presenter取data
        mPresenter.requestDatas();
    }

    @Override
    public void Permission_fail() {
        Toast.makeText(getActivity(), "权限授权失败无法使用", Toast.LENGTH_LONG).show();
        getActivity().finish();
    }
}
